//logs.js
const util = require('../../utils/util.js')
//获取应用实例
const app = getApp()
Page({
  data: {
    state: '',
    is_show: '', // show & hide
    is_do_relanch: ''
  },
  onLoad: function (param) {
    const self = this
    // 登录 获取code
    wx.login({
      success: res => {
        let _state = {}
        if (param && param.state) {
          _state = JSON.parse(decodeURIComponent(param.state))
        }
        _state.code = res.code || app.globalData.code
        self.setData({
          state: _state
        })
        self.getPayInfo(_state)
      }
    })
  },
  onShow: function () {
    const self = this
    self.setData({
      is_show: 'show'
    })
    if (self.data.is_do_relanch) {
      self.setData({
        is_do_relanch: ''
      })
      wx.reLaunch({
        url: '/pages/link/link'
      })
    }
  },
  onHide: function () {
    const self = this
    self.setData({
      is_show: 'hide'
    })
  },
  getPayInfo: function (_state) {
    const self = this
    wx.showLoading()
    wx.request({
      url: app.globalData.reqHost + '/Wx_Min/unifiedorder', // 获取支付信息
      method: 'GET',
      data: _state || {},
      success(res) {
        wx.hideLoading()
        self.toPay(res.data || {}) // 支付
      },
      fail(err) {
        wx.hideLoading({
          complete: function () {
            self.tipsToast('获取支付信息失败！', function () {
              app.globalData.page_link = app.globalData.reqHost + self.data.state.fail_redirect
              wx.reLaunch({
                url: '/pages/link/link'
              })
            })
          }
        })
      }
    })
  },
  toPay: function(param) {
    const self = this
    if (param.appId){
      wx.requestPayment({
        'timeStamp': param.timeStamp,
        'nonceStr': param.nonceStr,
        'package': param.package,
        'signType': param.signType,
        'paySign': param.paySign,
        'success': function (res) {
          app.globalData.page_link = app.globalData.reqHost + self.data.state.success_redirect
          if (self.data.is_show === 'show') {
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/link/link'
              })
            }, 200)
          } else {
            self.setData({
              is_do_relanch: 'success' // 保存下次进行跳转
            })
          }
        },
        'fail': function (res) {
          self.updateLog({
            name: '小程序支付报错',
            text: res.errMsg
          })
          app.globalData.page_link = app.globalData.reqHost + self.data.state.fail_redirect
          // andrio 报错
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/link/link'
            })
          }, 200)
        }
      })
    }
  },
  updateLog: function (param) {
    var self = this
    var _orderNum = ''
    var _user = ''
    if (self.data.state) {
      _orderNum = self.data.state.order_num
      _user = self.data.state.userid
    }
    // 写日志
    wx.request({
      url: app.globalData.reqHost + '/index.php/writeLog',
      data: {
        order_num: _orderNum || '',
        user: _user || '',
        name: param.name || '',
        text: param.text || ''
      },
      success(res) {},
      fail(err) {}
    })
  },
  tipsToast: function (title, callback) {
    wx.showToast({
      title: title || '获取信息失败！',
      icon: 'none',
      complete: function () {
        if (callback) {
          callback()
        }
      }
    })
    setTimeout(function() {
      wx.hideToast()
    }, 2000)
  }
})
